package tianrun.ziguan.api.calculate.config.controller.sync;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tianrun.ziguan.api.calculate.config.dto.request.sync.SyncBody;
import tianrun.ziguan.api.calculate.config.entity.TCalAsset;
import tianrun.ziguan.api.calculate.config.service.cal.asset.ITCalAssetService;
import tianrun.ziguan.api.calculate.config.service.sync.SyncService;
import tianrun.ziguan.api.calculate.config.utils.HttpResponseTools;
import tianrun.ziguan.api.common.core.base.ReturnMessage;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("sync")
@Api(tags = "同步")
public class SyncController {

    private final SyncService syncService;
    private final ITCalAssetService calAssetService;

    public SyncController(SyncService syncService, ITCalAssetService calAssetService) {
        this.syncService = syncService;
        this.calAssetService = calAssetService;
    }

    @ApiOperation(value = "同步")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("sync")
    public ResponseEntity<ReturnMessage> addOrEdit(@Valid @RequestBody SyncBody body) {
        syncService.doSync(body);
        return HttpResponseTools.good();
    }
    @ApiOperation(value = "同步(根据资产组编号)")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/sync/group")
    public ResponseEntity<ReturnMessage> saveByTree(@RequestBody Long groupId) {
        List<TCalAsset> list = calAssetService.list(Wrappers.<TCalAsset>lambdaQuery().eq(TCalAsset::getAssetGroupId, groupId));
        syncService.doSync(list);
        return HttpResponseTools.good();
    }
}
